home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / parted / geometry.py < prev    next >
Encoding:
Python Source  |  2010-06-29  |  7.6 KB  |  186 lines

  1. #
  2. # geometry.py
  3. # Python bindings for libparted (built on top of the _ped Python module).
  4. #
  5. # Copyright (C) 2009 Red Hat, Inc.
  6. #
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13. # Public License for more details.  You should have received a copy of the
  14. # GNU General Public License along with this program; if not, write to the
  15. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
  17. # source code or documentation are not subject to the GNU General Public
  18. # License and may only be used or replicated with the express permission of
  19. # Red Hat, Inc.
  20. #
  21. # Red Hat Author(s): Chris Lumens <clumens@redhat.com>
  22. #                    David Cantrell <dcantrell@redhat.com>
  23. #
  24.  
  25. import math
  26. import _ped
  27. import parted
  28.  
  29. from decorators import localeC
  30.  
  31. class Geometry(object):
  32.     """Geometry()
  33.  
  34.        Geometry represents a region on a device in the system - a disk or
  35.        partition.  It is expressed in terms of a starting sector and a length.
  36.        Many methods (read and write methods in particular) throughout pyparted
  37.        take in a Geometry object as an argument."""
  38.     @localeC
  39.     def __init__(self, device=None, start=None, length=None, end=None,
  40.                  PedGeometry=None):
  41.         """Create a new Geometry object for the given _ped.Device that extends
  42.            for length sectors from the start sector.  Optionally, an end sector
  43.            can also be provided."""
  44.         if PedGeometry:
  45.             self.__geometry = PedGeometry
  46.  
  47.             if device is None:
  48.                 self._device = parted.Device(PedDevice=self.__geometry.dev)
  49.             else:
  50.                 self._device = device
  51.         elif not end:
  52.             self._device = device
  53.             self.__geometry = _ped.Geometry(self.device.getPedDevice(), start, length)
  54.         elif not length and (end > start):
  55.             self._device = device
  56.             self.__geometry = _ped.Geometry(self.device.getPedDevice(), start, (end - start + 1), end=end)
  57.         elif start and length and end and (end > start):
  58.             self._device = device
  59.             self.__geometry = _ped.Geometry(self.device.getPedDevice(), start, length, end=end)
  60.         else:
  61.             raise parted.GeometryException, "must specify PedGeometry or (device, start, length) or (device, start, end) or (device, start, length, end)"
  62.  
  63.     def __eq__(self, other):
  64.         return not self.__ne__(other)
  65.  
  66.     def __ne__(self, other):
  67.         if hash(self) == hash(other):
  68.             return False
  69.  
  70.         if type(self) != type(other):
  71.             return True
  72.  
  73.         return self.device != other.device or self.start != other.start or self.length != other.length
  74.  
  75.     def __str__(self):
  76.         s = ("parted.Geometry instance --\n"
  77.              "  start: %(start)s  end: %(end)s  length: %(length)s\n"
  78.              "  device: %(device)r  PedGeometry: %(ped)r" %
  79.              {"start": self.start, "end": self.end, "length": self.length,
  80.               "device": self.device, "ped": self.__geometry})
  81.         return s
  82.  
  83.     @property
  84.     def device(self):
  85.         """The Device this geometry describes."""
  86.         return self._device
  87.  
  88.     start = property(lambda s: s.__geometry.start, lambda s, v: s.__geometry.set_start(v))
  89.     end = property(lambda s: s.__geometry.end, lambda s, v: s.__geometry.set_end(v))
  90.     length = property(lambda s: s.__geometry.length, lambda s, v: s.__geometry.set(s.__geometry.start, v))
  91.  
  92.     @localeC
  93.     def check(self, offset, granularity, count, timer=None):
  94.         """Check the region described by self for errors on the disk.
  95.            offset -- The beginning of the region to check, in sectors from the
  96.                      start of the geometry.
  97.            granularity -- How sectors should be grouped together
  98.            count -- How many sectors from the region to check."""
  99.         if not timer:
  100.             return self.__geometry.check(offset, granularity, count)
  101.         else:
  102.             return self.__geometry.check(offset, granularity, count, timer)
  103.  
  104.     @localeC
  105.     def contains(self, b):
  106.         """Return whether Geometry b is contained entirely within self and on
  107.            the same physical device."""
  108.         return self.__geometry.test_inside(b.getPedGeometry())
  109.  
  110.     @localeC
  111.     def containsSector(self, sector):
  112.         """Return whether the sectory is contained entirely within self."""
  113.         return self.__geometry.test_sector_inside(sector)
  114.  
  115.     @localeC
  116.     def getSize(self, unit="MB"):
  117.         """Return the size of the geometry in the unit specified.  The unit
  118.            is given as a string corresponding to one of the following
  119.            abbreviations:  b (bytes), KB (kilobytes), MB (megabytes), GB
  120.            (gigabytes), TB (terabytes).  An invalid unit string will raise a
  121.            SyntaxError exception.  The default unit is MB."""
  122.         lunit = unit.lower()
  123.         size = self.length * self.device.sectorSize
  124.  
  125.         if lunit not in parted._exponent.keys():
  126.             raise SyntaxError, "invalid unit %s given" % (unit,)
  127.  
  128.         return (size / math.pow(1024.0, parted._exponent[lunit]))
  129.  
  130.     @localeC
  131.     def intersect(self, b):
  132.         """Return a new Geometry describing the region common to both self
  133.            and Geometry b.  Raises ArithmeticError if the regions do not
  134.            intersect."""
  135.         return Geometry(PedGeometry=self.__geometry.intersect(b.getPedGeometry()))
  136.  
  137.     @localeC
  138.     def map(self, src, sector):
  139.         """Given a Geometry src that overlaps with self and a sector inside src,
  140.            this method translates the address of the sector into an address
  141.            inside self.  If self does not contain sector, ArithmeticError will
  142.            be raised."""
  143.         return parted.Geometry(PedGeometry=self.__geometry.map(src.getPedGeometry(), sector))
  144.  
  145.     @localeC
  146.     def overlapsWith(self, b):
  147.         """Return whether self and b are on the same device and share at least
  148.            some of the same region."""
  149.         try:
  150.             self.__geometry.intersect(b.getPedGeometry())
  151.             return True
  152.         except ArithmeticError:
  153.             return False
  154.  
  155.     @localeC
  156.     def read(self, offset, count):
  157.         """Read data from the region described by self.
  158.            offset -- The number of sectors from the beginning of the region
  159.                      (not the beginning of the disk) to read.
  160.            count  -- The number of sectors to read."""
  161.         return self.__geometry.read(offset, count)
  162.  
  163.     @localeC
  164.     def sync(self, fast=False):
  165.         """Flushes all caches on the device described by self.  If fast is
  166.            True, the flush will be quicked by cache coherency is not
  167.            guaranteed."""
  168.         if fast:
  169.             return self.__geometry.sync_fast()
  170.         else:
  171.             return self.__geometry.sync()
  172.  
  173.     @localeC
  174.     def write(self, buf, offset, count):
  175.         """Write data into the region described by self.
  176.            buf    -- The data to be written.
  177.            offset -- Where to start writing to region, expressed as the number
  178.                      of sectors from the start of the region (not the disk).
  179.            count  -- How many sectors of buf to write out."""
  180.         return self.__geometry.write(buf, offset, count)
  181.  
  182.     def getPedGeometry(self):
  183.         """Return the _ped.Geometry object contained in this Geometry.
  184.            For internal module use only."""
  185.         return self.__geometry
  186.